use std::net::UdpSocket;
use std::io::stdin;

//udp 服务端
pub fn udp_server(){
    let socket = UdpSocket::bind("127.0.0.1:8078").unwrap();
    //缓冲区
    let mut buf:[u8;1024] = [0u8; 1024];
    loop {
        let(size, addr)  = socket.recv_from(&mut buf).unwrap();
        println!("addr:{}", addr);
        let buf = &mut buf[..size];
        buf.reverse(); //反转
        socket.send_to(buf, &addr); //发送回去
    }
}
//udp客户端
pub fn udp_client(){
    let socket = UdpSocket::bind("127.0.0.1:8077").unwrap();
    socket.connect("127.0.0.1:8078").unwrap();
    let mut input = String::new();
    let mut buf: [u8; 1024] = [0u8; 1024];
    loop {
        input.clear();
        stdin().read_line(&mut input);
        socket.send(input.as_bytes());
        let(size, addr) = socket.recv_from(&mut buf).unwrap();
        println!("{}", String::from_utf8_lossy(&buf[..size]));
    }
}